package echat.future.cn.echat.message.rank;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import echat.future.cn.echat.R;
import echat.future.cn.echat.message.MessageApi;
import echat.future.cn.echat.message.rank.bean.RankGroupBean;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.UserHelper;
import lumingweihua.future.cn.baselibgxh.base.BaseFragment;

/**
 * Created by JX on 2018/8/19.
 */

public class RankFragment extends BaseFragment {

    private static final String TYPE = "type";

    private ViewPager mViewPager;
    private TextView tv_day;
    private TextView tv_week;
    private TextView tv_month;

    private int type;

    private MyAdapter mAdapter;

    public static RankFragment newInstance(int type) {
        RankFragment fragment = new RankFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.rank_fragment, container, false);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        type = getArguments().getInt(TYPE);

        tv_day = (TextView) findView(R.id.tv_day);
        tv_week = (TextView) findView(R.id.tv_week);
        tv_month = (TextView) findView(R.id.tv_month);

        tv_day.setOnClickListener(this);
        tv_week.setOnClickListener(this);
        tv_month.setOnClickListener(this);

        mViewPager = (ViewPager) findView(R.id.viewPager);
        mViewPager.setOffscreenPageLimit(2);
        mAdapter = new MyAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(1);

        if (type == 1) { //仙女榜
            new HttpUtilsRequst<RankGroupBean>(getActivity(), HttpUtilsRequst.retrofitClient.create(MessageApi.class)
                    .rank_xiannv(UserHelper.getToken()))
                    .setmOnRequstSuccessListener(new HttpUtilsRequst.OnRequstSuccessListener<RankGroupBean>() {
                        @Override
                        public void onSuccss(RankGroupBean rankBeans) {
                            mAdapter.setData(rankBeans);
                        }
                    }).start();
        } else if (type == 2) { //土豪榜
            new HttpUtilsRequst<RankGroupBean>(getActivity(), HttpUtilsRequst.retrofitClient.create(MessageApi.class)
                    .rank_tuhao(UserHelper.getToken()))
                    .setmOnRequstSuccessListener(new HttpUtilsRequst.OnRequstSuccessListener<RankGroupBean>() {
                        @Override
                        public void onSuccss(RankGroupBean rankBeans) {
                            mAdapter.setData(rankBeans);
                        }
                    }).start();
        }

    }

    @Override
    protected void processClick(View v) {
        switch (v.getId()) {
            case R.id.tv_day:
                mViewPager.setCurrentItem(0);
                tv_day.setBackgroundResource(R.drawable.rank_title_left_bg);
                tv_week.setBackgroundColor(0);
                tv_month.setBackgroundResource(0);
                tv_day.setTextColor(Color.parseColor("#FFFFFF"));
                tv_week.setTextColor(Color.parseColor("#999999"));
                tv_month.setTextColor(Color.parseColor("#999999"));
                break;
            case R.id.tv_week:
                mViewPager.setCurrentItem(1);
                tv_day.setBackgroundResource(0);
                tv_week.setBackgroundColor(Color.parseColor("#6560E2"));
                tv_month.setBackgroundResource(0);
                tv_day.setTextColor(Color.parseColor("#999999"));
                tv_week.setTextColor(Color.parseColor("#FFFFFF"));
                tv_month.setTextColor(Color.parseColor("#999999"));
                break;
            case R.id.tv_month:
                mViewPager.setCurrentItem(2);
                tv_day.setBackgroundResource(0);
                tv_week.setBackgroundColor(0);
                tv_month.setBackgroundResource(R.drawable.rank_title_right_bg);
                tv_day.setTextColor(Color.parseColor("#999999"));
                tv_week.setTextColor(Color.parseColor("#999999"));
                tv_month.setTextColor(Color.parseColor("#FFFFFF"));
                break;
        }
    }

    private class MyAdapter extends FragmentPagerAdapter {

        private RankItemFragment dayFragment;
        private RankItemFragment weekFragment;
        private RankItemFragment monthFragment;

        public MyAdapter(FragmentManager fm) {
            super(fm);
            dayFragment = RankItemFragment.newInstance(type);
            weekFragment = RankItemFragment.newInstance(type);
            monthFragment = RankItemFragment.newInstance(type);
        }

        public void setData(RankGroupBean data) {
            dayFragment.setData(data.getDay_rank());
            weekFragment.setData(data.getWeek_rank());
            monthFragment.setData(data.getMonth_rank());
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return dayFragment;
            } else if (position == 1) {
                return weekFragment;
            }
            return monthFragment;
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

}
